//
//  MYGChangeEnterMessageTitleCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/10.
//

import UIKit

class MYGChangeEnterMessageTitleCell: UITableViewCell {
    // MARK: - Property
    var model: MYGChangeEnterMessageTextCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            nameLabel.text = model.name
            requiredLabel.isHidden = !model.isRequeired
            textField.attributedPlaceholder = NSMutableAttributedString.rz.colorfulConfer(confer: { confer in
                confer.text(model.placeholder)?.font(.regular(16)).textColor(.rgba(204, 204, 204, 1))
            })
            textField.text = model.content
        }
    }
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .white
        
        contentView.addSubview(stackView)
        contentView.addSubview(textField)
        contentView.addSubview(rightImageView)
        contentView.addSubview(lineView)
        stackView.addArrangedSubviews([requiredLabel, nameLabel])
        stackView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(26)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(20)
        }
        
        textField.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.top.equalTo(stackView.snp.bottom)
            make.bottom.equalToSuperview()
        }
        
        rightImageView.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(36)
            make.centerY.equalToSuperview()
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    // MARK: - lazy load
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .horizontal
        stackV.alignment = .leading
        return stackV
    }()
    
    private lazy var requiredLabel: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(255, 0, 0, 1)
        label.font = .regular(16)
        label.text = "*"
        return label
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(51, 51, 51, 1)
        label.font = .regular(16)
        label.text = "头像"
        return label
    }()
    
    private lazy var textField: UITextField = {
        let tf = UITextField()
        tf.font = .regular(16)
        tf.textColor = .rgba(51, 51, 51)
        
        tf.delegate = self
        tf.addTarget(self, action: #selector(textFieldValueChanged), for: .editingChanged)
        return tf
    }()
    
    private lazy var rightImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "re_next"))
        return imgV
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238, 1)
        return view
    }()
}

extension MYGChangeEnterMessageTitleCell: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        model?.clickAction?()
        return model?.canEdit ?? false
    }
    
    func textFieldDidChangeSelection(_ textField: UITextField) {
        model?.content = textField.text ?? ""
    }
    
    @objc
    func textFieldValueChanged() {
        model?.content = textField.text ?? ""
    }
}
